import 'dart:convert' show json;

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class User {
  User({
    this.id,
    this.nickname,
    this.portrait,
    this.desc,
    this.isFollowed,
    this.userId,
    this.background,
  });

  factory User.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : User(
          id: asT<int>(jsonRes['user_id']),
          nickname: asT<String>(jsonRes['nickname']),
          portrait: asT<String>(jsonRes['portrait']),
          desc: asT<String>(jsonRes['desc']),
          isFollowed: asT<int>(jsonRes['is_followed']),
          userId: asT<int>(jsonRes['user_id']),
          background: asT<String>(jsonRes['background']));

  int id;
  String nickname;
  String portrait;
  String desc;
  int isFollowed;
  int userId;
  String background;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'nickname': nickname,
        'portrait': portrait,
        'desc': desc,
        'is_followed': isFollowed,
        'user_id': userId,
        'background': background,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
